package com.example.service.impl.weixiubaoyang;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.result.PageResult;
import com.example.entity.dto.weixiubaoyang.ReceiptMouldPageDto;

import com.example.entity.dto.weixiubaoyang.ReturnMouldPageDto;
import com.example.entity.vo.weixiubaoyang.ReceiptMouldEqtttVo;
import com.example.entity.vo.weixiubaoyang.ReceiptMouldVo;
import com.example.entity.vo.weixiubaoyang.ReturnMouldVo;
import com.example.entity.yifei.Eqtts;
import com.example.entity.yifei.EqtttD;
import com.example.entity.yifei.Eqttu;
import com.example.mapper.EqttsMapper;
import com.example.mapper.EqtttDMapper;
import com.example.mapper.EqtttMapper;
import com.example.mapper.EqttuMapper;
import com.example.service.EqttsService;
import com.example.service.weixiubaoyang.ReceiptService;
import com.example.untils.BeanUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ReceiptServiceImpl implements ReceiptService {

    @Autowired
    private EqttsService eqttsService;

    @Autowired
    private EqttsMapper eqttsMapper;

    @Autowired
    private EqtttMapper eqtttMapper;

    @Autowired
    private EqttuMapper eqttuMapper;
    @Autowired
    private EqtttDMapper eqtttDMapper;



    /**
     * 分页获取单头详细信息
     * @param receiptMouldPageDto
     * @return
     */
    @Override
    public PageResult<Eqtts> pageQueryEqtts(ReceiptMouldPageDto receiptMouldPageDto) {
        // 设置分页
        PageHelper.startPage(receiptMouldPageDto.getPage(),receiptMouldPageDto.getPageSize());
        // 分页条件查询
        Page<Eqtts> eqttsList = eqttsMapper.pageQueryEqtts(receiptMouldPageDto);

        return new PageResult(eqttsList.getTotal(),eqttsList.getResult());
    }

    /**
     * 领模单详情
     * @param TS001
     * @param TS002
     * @return
     */
    @Override
    public ReceiptMouldVo details(String TS001, String TS002) {
        //根据单别，单号查询单头
        Eqtts eqtts = eqttsService.getOne(new LambdaQueryWrapper<Eqtts>()
                .eq(Eqtts::getTS001, TS001)
                .eq(Eqtts::getTS002, TS002));

        //查询单头对应的单身
        List<ReceiptMouldEqtttVo> eqtttVoList = eqtttMapper.getByTS001And2(TS001, TS002);
        //将单头和单身存入ReceiptMouldVo并返回
        ReceiptMouldVo receiptMouldVo = new ReceiptMouldVo();
        BeanUtils.copyPropertiesIgnoreCase(eqtts, receiptMouldVo);
        receiptMouldVo.setEqtttVoList(eqtttVoList);

        // 查询子单身
        for (ReceiptMouldEqtttVo eqtttVo : eqtttVoList) {
            eqtttVo.setSonList(eqtttDMapper.selectList(new LambdaQueryWrapper<EqtttD>().eq(EqtttD::getTtd001, eqtttVo.getTT001())
                    .eq(EqtttD::getTtd002, eqtttVo.getTT002()).eq(EqtttD::getTtd003, eqtttVo.getTT003())));
        }
        return receiptMouldVo;
    }

    /**
     * 退模单头信息
     * @param returnMouldPageDto
     * @return
     */
    @Override
    public PageResult<Eqttu> pageQueryEqttu(ReturnMouldPageDto returnMouldPageDto) {
        PageHelper.startPage(returnMouldPageDto.getPage(), returnMouldPageDto.getPageSize());
        Page<Eqttu> eqttuList = eqttuMapper.pageQueryEqttu(returnMouldPageDto);
        return new PageResult(eqttuList.getTotal(),eqttuList.getResult());
    }

    /**
     * 退模单详情
     * @return
     */
    @Override
    public ReturnMouldVo rdetails() {

        return null;
    }

}
